
Ext.Loader.setConfig({enabled: true});
Ext.Loader.setPath('gestionConf', 'static/combos/componentes/gestionConf');

Ext.require([
    'Ext.data.*', 'Ext.grid.*','Ext.tab.*','Ext.tip.QuickTipManager',
    'Ext.window.MessageBox','Ext.window.*', 'Ext.tip.*',
    'Ext.layout.container.Border', 'Ext.dd.*', 'Ext.form.field.ComboBox',
    'gestionConf.FaseForm','gestionConf.FaseGrid',
    'gestionConf.LBForm','gestionConf.LBGrid',
    'gestionConf.ProyectoForm','gestionConf.ProyectoGrid',
    'gestionConf.ItemForm', 'gestionConf.ItemGrid',  
    'Ext.form.FieldSet'
]);

Ext.define('Writer.Atributo', {
    extend: 'Ext.data.Model',
    fields: [{
        name: 'id',
        type: 'int',
        useNull: true
    }, 'nombreAtributo', 'descripcion', 'tipo','valor']
});

Ext.onReady(function(){
    Ext.tip.QuickTipManager.init();  
        
		
    var store = Ext.create('Ext.data.Store', {
        model: 'Writer.Person',
        autoLoad: true,
        autoSync: true,
        proxy: {
            type: 'ajax',
            url: 'listar_proyectos_gestion',
            reader: {
                type: 'json',
                successProperty: 'success',
                root: 'data',
                messageProperty: 'message'
            },
        },        
        sorters: [{
            property: 'id',
            direction: 'ASC' }]        
    }); 
       
	win = Ext.create('widget.panel', {
		iconCls: 'icon-alpha',	title: 'GESTION DE CONFIGURACION',
		frame:true,
		renderTo: 'comboGestionConf',	width: 1055,  height: 390,	layout: 'border', y: 30,	x: 160, //bodyStyle: 'padding: 3px;',
		items: [{
			region: 'west',	xtype: 'writerform',
			itemId: 'form',	width: 360,
			listeners: {
				refresh: function() {	win.child('#grid').onRefresh();
				},
				ddFases: function(data) {
					nombre = extraer(data, '&nombre=', '&descripcion=');		id = extraer(data, 'id=', '&');
					win.hide();
					winFases = Ext.create('widget.window', {
						iconCls: 'icon-alpha',
						y: 221,	x: 164,
						title: 'FASES',
						closable: false,	
						layout: 'fit',
						items: [PanelFases = Ext.create('Ext.Panel', {
									width : 1030,	height : 340,
									layout  : {	type: 'hbox',	align: 'stretch'	},
									items : [								
										{
											region: 'west',
											xtype: 'fasesform',
											title: 'Fases - ' + nombre,
											itemId: 'Faseform',
											width: 350,
											listeners: {
												refresh: function() {
													PanelFases.child('#fasesgrid').onRefresh();		
												},
												ddItem: function(data) {
													winFases.hide();
													nombre = extraer(data, '&nombreFase=', '&descripcion=');		id = extraer(data, 'id=', '&');
													winItem = Ext.create('widget.window', {
														iconCls: 'icon-alpha',
														y: 221,	x: 164,								
														title: 'ITEMS',
														closable: false,	
														plain: true,
														items: [PanelItem = Ext.create('Ext.Panel', {
																	width : 1030,	height : 340,
																	layout  : {	type: 'hbox',	align: 'stretch'	},
																	items : [								
																		{
																			region: 'west',
																			xtype: 'itemform',
																			title: 'Items -'+ nombre,
																			itemId: 'itemform',
																			width: 350,
																			listeners: {
																				refresh: function() {
																					PanelItem.child('#itemgrid').onRefresh();		
																				},
																				reset: function() {
																					PanelItem.child('#itemgrid').getSelectionModel().clearSelections();
																					
																				},
																				AtributosItem: function(data){
																					var gridAtributosRevivir;
																					nombre = extraer(data, '&nombre=', '&descripcion='); idItem = extraer(data, 'id=', '&');
																					winAtributosItem = Ext.create('widget.window', {
																						iconCls: 'icon-atributos',
																						x: 450, y: 300,
																						title: 'Atributos del Item: ' + nombre,
																						closable: false,
																						plain: true,
																						layout: 'fit',
																						items: [PanelAtributosRevivir = Ext.create('Ext.Panel', {
																									width : 450,	height : 200,
																									layout  : {	type: 'hbox',	align: 'stretch'	},
																									defaults  : { flex : 1 }, 
																									items : [
																											gridAtributosRevivir = Ext.create('Ext.grid.Panel', {
																																																								
																												store: Ext.create('Ext.data.Store', {
																															model: 'Writer.Atributo',
																															autoLoad: true,
																															proxy: {
																																type: 'ajax',
																																url: 'listarAtributos_Item/'+ idItem,
																																reader: {
																																	type: 'json',
																																	successProperty: 'success',
																																	root: 'data',
																																	messageProperty: 'message'
																																},

																															}      
																														}),
																												columns:  [ {text: "Id", hidden:true, dataIndex: 'id'},
																															{text: "<b>Nombre</b>", width: 120, dataIndex: 'nombreAtributo'},
																															{text: "<b>Descripcion</b>", flex: 1, dataIndex: 'descripcion'}, 
																															{text: "<b>Tipo</b>", width: 100, dataIndex: 'tipo'},
																															{text: "<b>Valor</b>", width: 100, dataIndex: 'valor'}],																															
																															
																												stripeRows : true
																											})
																									],
																									dockedItems: {
																										xtype: 'toolbar',
																										dock: 'bottom',
																										items: ['->', // Fill
																										{
																											text: 'Cerrar',
																											iconCls: 'icon-cerrar',
																											handler: function(){winAtributosItem.close()}
																										}]
																									}
																							}),{
																							border: false
																						}]
																					});					
																					winAtributosItem.show();						
																				}																				
																			}
																			
																		}, {
																			region: 'center',
																			xtype: 'itemgrid',            
																			itemId: 'itemgrid',
																			title: 'Lista de Items',
																			flex: 1,        
																			store: Ext.create('Ext.data.Store', {
																					model: 'Writer.Item',
																					autoLoad: true,
																					//autoSync: true,
																					proxy: {
																						type: 'ajax',
																						url: 'listar_items_por_fases_gestion/'+ id,
																						reader: {
																							type: 'json',
																							successProperty: 'success',
																							root: 'data',
																							messageProperty: 'message'
																						},
																						writer: {
																							type: 'json',
																							writeAllFields: false,
																							root: 'data'
																						},

																					},
																					
																					sorters: [{
																						property: 'numero',
																						direction: 'ASC'
																					}]        
																				}),
																			listeners: {
																				selectionchange: function(selModel, selected) {
																					PanelItem.child('#itemform').setActiveRecord(selected[0] || null);
																				},
																				salir: function() {
																					winItem.close();
																					winFases.show();
																					PanelFases.child('#fasesgrid').onRefresh();
																				}
																			}
																		}]
																	})
																	
															]
													});	
													PanelItem.child('#itemform').down('#idFase').setValue(id);
													
													winItem.show();
																		
												},
												fireLB: function(data) {
													winFases.hide();
													nombre = extraer(data, '&nombreFase=', '&descripcion=');		id = extraer(data, 'id=', '&');
													winLB = Ext.create('widget.window', {
														iconCls: 'icon-alpha',
														y: 221,	x: 164,								
														title: 'LINEAS BASE',
														closable: false,	
														plain: true,
														items: [PanelLB = Ext.create('Ext.Panel', {
																	width : 1030,	height : 340,
																	layout  : {	type: 'hbox',	align: 'stretch'	},
																	items : [								
																		{
																			region: 'west',
																			xtype: 'LBform',
																			title: 'Lineas Base -'+ nombre,
																			itemId: 'lbform',
																			width: 350,
																			listeners: {
																				refresh: function() {
																					PanelLB.child('#lbgrid').onRefresh();		
																				}/*
																				,reset: function() {
																					PanelItem.child('#lbgrid').getSelectionModel().clearSelections();
																					
																				}*/
																				,
																				ddItems: function(data) {
																					//winFases.hide();
																					idLB = extraer(data, 'id=', '&');
																					winItem = Ext.create('widget.window', {
																						iconCls: 'icon-alpha',
																						y: 270,	x: 340,								
																						title: 'ITEMS',
																						closable: false,	
																						plain: true,
																						items: [PanelItem = Ext.create('Ext.Panel', {
																									width : 700,	height : 280,
																									layout  : {	type: 'hbox',	align: 'stretch'	},
																									items : [								
																										{
																											region: 'center',
																											xtype: 'itemgrid',            
																											itemId: 'itemgrid',
																											title: 'Lista de Items',
																											flex: 1,        
																											store: Ext.create('Ext.data.Store', {
																													model: 'Writer.Item',
																													autoLoad: true,
																													proxy: {
																														type: 'ajax',
																														url: 'listar_items_linea/'+ idLB,
																														reader: {
																															type: 'json',
																															successProperty: 'success',
																															root: 'data',
																															messageProperty: 'message'
																														},
																														writer: {
																															type: 'json',
																															writeAllFields: false,
																															root: 'data'
																														},

																													},
																													
																													sorters: [{
																														property: 'numero',
																														direction: 'ASC'
																													}]        
																												}),
																											listeners: {
																												selectionchange: function(selModel, selected) {
																													PanelItem.child('#itemform').setActiveRecord(selected[0] || null);
																												},
																												salir: function() {
																													winItem.close();
																													
																												}
																											}
																										}]
																									})
																									
																							]
																					});	
																					winItem.show();
																										
																				},
																				ddGenerar: function(data) {
																					var grid7, grid8;
																					winGenerar = Ext.create('widget.window', {
																						iconCls: 'icon-generar',
																						x: 400, y: 260,
																						title: 'Drag and Drop - Generar Linea Base: ' + nombre,
																						closable: false,
																						plain: true,
																						layout: 'fit',
																						items: [PanelGenerar = Ext.create('Ext.Panel', {
																									width : 550,	height : 250,
																									layout  : {	type: 'hbox',	align: 'stretch'	},
																									defaults  : { flex : 1 }, 
																									items : [
																											grid7 = Ext.create('Ext.grid.Panel', {
																												multiSelect: true,
																												viewConfig: {
																													plugins: {
																															ptype: 'gridviewdragdrop',
																															dragGroup: 'firstGridDDGroup',
																															dropGroup: 'secondGridDDGroup'
																													},
																												},
																												store: Ext.create('Ext.data.Store', {
																															model: 'DataItem',
																															autoLoad: true,
																															proxy: {
																																type: 'ajax',
																																url: 'items_para_lb/' + id,
																																reader: {
																																	type: 'json',
																																	successProperty: 'success',
																																	root: 'data',
																																	messageProperty: 'message'
																																},

																															}      
																														}),
																												columns:  [ {text: "Id", hidden:true, dataIndex: 'id'},
																															{text: "<b>Items Disponibles</b>", flex: 1, sortable: true, dataIndex: 'nombreItem'} ],
																												stripeRows : true
																											}),
																											grid8 = Ext.create('Ext.grid.Panel', {
																												multiSelect: true,
																												viewConfig: {
																													plugins: {
																															ptype: 'gridviewdragdrop',
																															dragGroup: 'secondGridDDGroup',
																															dropGroup: 'firstGridDDGroup'
																														},
																												},
																												store: Ext.create('Ext.data.Store', {
																														model: 'DataItem',
																														autoLoad: true,
																														proxy: {
																															type: 'ajax',
																															url: 'vacio',
																															reader: {
																																type: 'json',
																																successProperty: 'success',
																																root: 'data',
																																messageProperty: 'message'
																															},

																														}       
																												}),
																												columns  : [ {text: "Id", hidden:true, dataIndex: 'id'},
																															{text: "<b>Items LB</b>", flex: 1, sortable: true, dataIndex: 'nombreItem'} ],
																												stripeRows : true
																											})
																									],
																									dockedItems: {
																										xtype: 'toolbar',
																										dock: 'bottom',
																										items: ['->', // Fill
																										{
																											text: 'Guardar',
																											iconCls: 'icon-save',
																											handler: function(){
																												grid8.getSelectionModel().selectAll();
																												longitud= grid8.getSelectionModel().getSelection().length;
																												
																													lista_Item = new Array(longitud);
																													i=0;
																													while(i < longitud)
																													{											
																														lista_Item[i]=grid8.getSelectionModel().getSelection()[i].data['id'];
																														i=i+1;
																													}	
																													grid8.getSelectionModel().deselectAll();											
																													Ext.Ajax.request({
																														url: 'generar_linea_base',
																														params: {
																															idFase: id,
																															lista_Item:Ext.encode(lista_Item)
																														},
																														success: function(response) {
																															var jsonData = Ext.JSON.decode(response.responseText);
																															var resultMessage = jsonData['mensaje'];
																															Ext.Msg.show({ 		title: '<br><p align="center"><b>ALPHA MANAGER</b></p>', //<- el título del diálogo  
																																				msg: resultMessage, 	icon: 'ext-mb-ok',
																															});		
																															winGenerar.close();
																															PanelLB.child('#lbgrid').onRefresh();
																														},
																														failure: function(response) {
																															var jsonData = Ext.JSON.decode(response.responseText);
																															var resultMessage = jsonData['mensaje'];
																															Ext.Msg.show({ 		title: '<br><p align="center"><b>ALPHA MANAGER</b></p>', //<- el título del diálogo  
																																				msg: resultMessage, 	icon: 'ext-mb-error',
																															});	/*
																															Ext.Msg.show({	title: '<br><p align="center"><b>ALPHA MANAGER</b></p>',   
																																			msg: 'Error en la operacion Generar Linea Base', 	icon: 'ext-mb-error',
																															});*/
																														}
																													})
																												
																											}
																										},
																										{
																											text: 'Cerrar',
																											iconCls: 'icon-cerrar',
																											handler: function(){winGenerar.close()}
																										}]
																									}
																							}),{
																							border: false
																						}]
																					});					
																					winGenerar.show();		
																				}
																			}
																			
																		}, {
																			region: 'center',
																			xtype: 'LBgrid',            
																			itemId: 'lbgrid',
																			title: 'Lista de Lineas Base',
																			flex: 1,        
																			store: Ext.create('Ext.data.Store', {
																					model: 'Writer.LB',
																					autoLoad: true,
																					//autoSync: true,
																					proxy: {
																						type: 'ajax',
																						url: 'listar_lineas_base/'+ id,
																						reader: {
																							type: 'json',
																							successProperty: 'success',
																							root: 'data',
																							messageProperty: 'message'
																						},
																						writer: {
																							type: 'json',
																							writeAllFields: false,
																							root: 'data'
																						},

																					},
																					
																					sorters: [{
																						property: 'numero',
																						direction: 'ASC'
																					}]        
																				}),
																			listeners: {
																				selectionchange: function(selModel, selected) {
																					PanelLB.child('#lbform').setActiveRecord(selected[0] || null);
																				},
																				salir: function() {
																					winLB.close();
																					winFases.show();
																					PanelFases.child('#fasesgrid').onRefresh();
																				}
																			}
																		}]
																	})
																	
															]
													});	
													//PanelLB.child('#lbform').down('#idFase').setValue(id);
													
													winLB.show();
																		
												}
											}
											//height: 150,
										}, {
											region: 'center',
											xtype: 'fasesgrid',            
											itemId: 'fasesgrid',
											title: 'Lista de Fases',
											flex: 1,        
											//width: 480,
											//height: 250,
											store: Ext.create('Ext.data.Store', {
													model: 'Writer.Fases',
													autoLoad: true,
													autoSync: true,
													proxy: {
														type: 'ajax',
														//url: 'listar_fases_por_proyecto/'+ id,
														url: 'listar_fases_gestion/'+ id,
														reader: {
															type: 'json',
															successProperty: 'success',
															root: 'data',
															messageProperty: 'message'
														},
														writer: {
															type: 'json',
															writeAllFields: false,
															root: 'data'
														},

													},
													
													sorters: [{
														property: 'posicionFase',
														direction: 'ASC'
													}]        
												}),
											listeners: {
												selectionchange: function(selModel, selected) {
													PanelFases.child('#Faseform').setActiveRecord(selected[0] || null);
												},
												salir: function() {
													winFases.close();
													win.show();
													win.child('#grid').onRefresh();
												}
											}
										}]
									})
									
							]
					});	
					PanelFases.child('#Faseform').down('#idProy').setValue(id);
					/*displayPanel.child('#Faseform').down('#tipo').store = Ext.create('Ext.data.Store', {
																				model: 'DataTipoItem',
																				autoLoad: false,
																				proxy: {
																					type: 'ajax',
																					url: 'listar_tipo_items_por_fase/'+'1',
																					reader: {
																						type: 'json',
																						successProperty: 'success',
																						root: 'data',
																						messageProperty: 'message'
																					}
																				}        
																				
																			});*/
					winFases.show();
										
				}						
			}
		}, {
			region: 'center',
			xtype: 'writergrid',            
			itemId: 'grid',
			title: 'Lista de Proyectos',

			flex: 1,        
			//width: 750,
			//height: 380,

			store: store,
			listeners: {
				selectionchange: function(selModel, selected) {
					win.child('#form').setActiveRecord(selected[0] || null);
				}
			}
		}]
	});
	win.show();
	//getPermisos();
    

    
});

function extraer(texto, etqini, etqfin){
		var ind0, ind1, parte ="";
		ind0 = texto.indexOf(etqini);
		if (ind0 >=0){
			ind1 = texto.indexOf(etqfin);
			if (ind1>ind0)
				parte = texto.substring(ind0+etqini.length, ind1);
		}
		return parte;
}
